<?php

namespace app\api\controller;


use app\admin\model\Address;
use app\admin\model\Luck;
use app\admin\model\Songdetail;
use app\common\controller\Api;
use app\admin\model\Song;
use app\admin\model\User;
use app\admin\model\Order;
use app\admin\model\Orderdetail;
use think\Db;
class Delivery extends Api
{
    protected $noNeedLogin = ["*"];
    private $user;
    public function __construct()
    {
        parent::__construct();
        $token = $this->request->header('token','9f66fbb7-4e27-4f6f-a4a4-2cdf237548c7');
      
        $this->user = Song::where(['token' => $token])->find();
     
       
    }
    //我的订单
    public function myOrder()
    {
        if(empty($this->user)) $this->error('请先登录',['code' => 401]);
        $where['delivery_user_id'] = $this->user->id;
        $page  = $this->request->get('page',1); 
        $status = $this->request->get('status','');
        if($status) {
            $where['status'] = $status;
        }
        $time = $this->request->get('time','');
        if($time) {
            $beginTime = strtotime($time.' 00:00:00');
            $endTime =  strtotime($time.' 23:59:59');
            $where['create_time'] = ['between',[$beginTime,$endTime]];
        }
       
        $list = Order::where( $where)->order('id desc')->paginate(20,false,['page' => $page])->each(function(&$item){
            $detail = Orderdetail::where(['order_sn' => $item['order_id']])->select();
            foreach($detail as $key => $value ) {
                $detail[$key]['images'] = dealImages($this->request->domain(),$value['images']);
            }
            $item['addressDetail']  = Address::find($item['address_id']);
            $item['orderDetail'] = $detail;
        });
       
        $this->success('suc',$list);
    }
    public function delivery()
    {
        if(empty($this->user)) $this->error('请先登录',['code' => 401]);
        $orderId = $this->request->post('id',0);//订单id
        if(!$orderId) $this->error('参数错误');
        $orderDetail = Order::find($orderId);
        if(empty($orderDetail)) $this->error('订单错误');
        if($orderDetail->status == 3) {
            $this->error('无需重复处理');
        }
        $code = $this->request->post('code','');
        if($orderDetail['active'] == 1) {//如果是活动商品
            if(!$code) $this->error('参数错误');
            if($orderDetail->code != $code) $this->error('核销码错误');
        }
        Db::startTrans();
        try{
            $orderDetail->status = 3;
            $this->user->num = $this->user->num + 1;
            $orderDetail->delivery_to_time = time();
       
            $orderDetail->save();
            $this->user->save();
            $user = User::find($orderDetail['user_id']);
            Luck::where(['id' =>  $orderDetail['luck_id']])->update(['status' => 3]);
            Song::common($user, $orderDetail['order_id'],'已送达');
            Songdetail::where(['order_id' => $orderId,'user_id' => $this->user->id])->update(['end_time' => time()]);
            Db::commit();
            $this->success('suc');
        }catch(\Exception $e){
            $error = $e->getMessage();
            if($error) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('suc');
        }

    }
    /**
     * 点击开始配送
     * Summary of begin
     * @return void
     */
    public function begin($id = '')
    {
        if(empty($this->user)) $this->error('请先登录',['code' => 401]);
        $detail = Order::find($id);
        if(empty($detail)) $this->error('订单不存在');

        if($detail['delivery_user_id'] != $this->user->id) {
            if(empty($detail)) $this->error('只能点击分配给自己的订单');
        }
        $detail->status = 2;
        $detail->save();
        Songdetail::where(['order_id' => $id,'user_id' => $this->user->id])->update(['begin_time' => time()]);
        
        $this->success('suc');
    }

}
